package org.swidgets.sgrid;

import org.swidgets.base.Result;
import org.swidgets.base.SColumn;


public abstract class SimpleStringArrayTableModel extends AbstractTableModel {

	private String[][] data;
	private int totalRecords;
	private int currentRecords;
	
	
	public String[] getColumnName(){
		SColumn[] columns = getMetaData();
		String[] columnName = new String[columns.length];
		
		for(int i=0;i<columnName.length;i++){
			columnName[i] = columns[i].getName();
		}
		
		return columnName;
	}
	
	public String[] getColumnWidth(){
		return null;
	}

	
	public Object getCellData(int row, int col) {
		
		return data[row][col];
	}
	
	public Object getRowData(int row){
		return data[row];
	}

	public void setCellData(Object value, int row, int col) {
		
		data[row][col] = (String) value;
	}

	public SColumn[] getMetaData() {

		String[] columnName = getColumnName();
		String[] columnWidth = getColumnWidth();
		SColumn[] columns = new SColumn[columnName.length];
		if(columnWidth==null){
			for(int i=0;i<columnName.length;i++){
				columns[i] = new SColumn(columnName[i]);
			}
		}
		else{
			for(int i=0;i<columnName.length;i++){
				columns[i] = new SColumn(columnName[i],columnWidth[i]);
			}
		}
		return columns;
	}

	public void setData(Result result) {
		
		this.data = (String[][]) result.getData();
		this.totalRecords = result.getTotalRecords();
		this.currentRecords=result.getCurrentRecords();
	}

	public int getTotalRecords() {
		return totalRecords;
	}
	
	public int getCurrentRecords(){
		return currentRecords;
	}
	
}
